<?php

namespace app\api\controller;

use app\api\logic\CustomersLogic;
use app\common\basic\BaseController;
use support\Request;
use support\Response;

class CustomerController extends BaseController
{
    protected $user;
    public function __construct()
    {
        $this->logic = new CustomersLogic();
        $this->user = request()->user ?? null;
        parent::__construct();
    }

    public function lists(Request $request)
    {
        $params = $request->more([
            ['page', 1],
            ['limit', 10],
            ['name', ''],
            ['phone', ''],
            ['province_id', 0],
            ['city_id', 0],
            ['area_id', 0],
            ['stage', 0],
            ['create_time', []],
            ['user_id', $this->user['id'] ?? 0],
        ]);
        if (!empty($params['create_time'])) {
            $params['create_time'] = parseDateRange($params['create_time'], '/', true, 'Y-m-d H:i:s');
        } else {
            unset($params['create_time']);
        }
        $result = $this->logic->getCustomerList($params);
        return $this->success($result, '查询成功');
    }

    public function create(Request $request)
    {
        try {
            $data = $request->post();
            $data['user_id'] = $this->user?->id ?? 0;
            $result = $this->logic->saveCustomer($data);
            return $this->success($result, '保存成功');
        } catch (\Exception $e) {
            $error = $e->getMessage();
            // 处理JSON格式的字段验证错误
            if (strpos($error, '{') === 0) {
                $errors = json_decode($error, true);
                if (is_array($errors)) {
                    return $this->fail('字段验证失败', $errors);
                }
            }
            return $this->fail($error);
        }
    }

     /**
     * 修改数据
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function update(Request $request, $id) : Response
    {
        if (empty($id)) {
            return $this->fail('参数错误，请检查');
        }
        $data = $request->post();
        $result = $this->logic->updateCustomer($id, $data);
        if (!$result) {
            return $this->fail('修改失败');
        }
        return $this->success($result, '修改成功');
    }

    public function customerInfo(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            return $this->fail('参数错误，请检查');
        }
        $scene = $request->input('scene', '');
        $result = $this->logic->getCustomerInfo($id, $scene);
        if (!$result) {
            return $this->fail('查询失败');
        }
        return $this->success($result, '查询成功');
    }

    public function customerDetail(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            return $this->fail('参数错误，请检查');
        }
        $scene = $request->input('scene', '');
        $result = $this->logic->getCustomerDetail($id, $scene);
        if (!$result) {
            return $this->fail('查询失败');
        }
        return $this->success($result, '查询成功');
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            return $this->fail('参数错误，请检查');
        }
        $result = $this->logic->deleteCustomer($id);
        if (!$result) {
            return $this->fail('删除失败');
        }
        return $this->success($result, '删除成功');
    }
}